home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / mc-3.2 / mc-3 / mc-3.2.1 / vfs / utilvfs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-17  |  4.7 KB  |  177 lines

  1. /* Utilities for VFS modules.
  2.  
  3.    Currently includes login and tcp open socket routines.
  4.    
  5.    Copyright (C) 1995, 1996 Miguel de Icaza
  6.    
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20. #include <config.h>
  21. #include <unistd.h>
  22. #include <stdlib.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include <signal.h>
  26. #include <pwd.h>
  27. #include <sys/types.h>
  28. #include <netdb.h>
  29. #include <sys/socket.h>
  30. #include <netinet/in.h>
  31. #include <arpa/inet.h>
  32. #include <malloc.h>
  33. #ifdef USE_TERMNET
  34. #include <termnet.h>
  35. #endif
  36.  
  37. #include <signal.h>
  38. #include <errno.h>
  39. #include "tcputil.h"
  40. #include "../src/dialog.h"    /* for message () */
  41. #include "../src/mem.h"        /* for bcopy */
  42. #include "../src/util.h"    /* for unix_error_string */
  43. #include "../src/mad.h"
  44.  
  45. int open_tcp_link  (char *host, int *port, char *caller)
  46. {
  47.     struct   sockaddr_in server_address;
  48.     unsigned long inaddr;
  49.     struct   hostent *hp;
  50.     int      my_socket;
  51.  
  52.     if (!*host)
  53.     return 0;
  54.     
  55.     bzero ((char *) &server_address, sizeof (server_address));
  56.     server_address.sin_family = AF_INET;
  57.     
  58.     /*  Try to use the dotted decimal number */
  59.     if ((inaddr = inet_addr (host)) != -1)
  60.     bcopy ((char *) &inaddr, (char *) &server_address.sin_addr,
  61.            sizeof (inaddr));
  62.     else {
  63.     if ((hp = gethostbyname (host)) == NULL){
  64.         message (1, caller, " Can't locate hostname: %s ", host);
  65.         return 0;
  66.     }
  67.     bcopy ((char *) hp->h_addr, (char *) &server_address.sin_addr,
  68.            hp->h_length);
  69.     }
  70.  
  71.     /* Try to contact a remote portmapper to obtain the listening port */
  72.     if (*port == 0){
  73.     *port = get_remote_port (&server_address);
  74.     if (*port < 1)
  75.         return 0;
  76.     }
  77.     server_address.sin_port = htons (*port);
  78.     
  79.     if ((my_socket = socket (AF_INET, SOCK_STREAM, 0)) < 0){
  80.     message (1, caller, " Can't create socket: %s ",
  81.          unix_error_string(errno));
  82.     return 0;
  83.     }
  84.     if (connect (my_socket, (struct sockaddr *) &server_address,
  85.          sizeof (server_address)) < 0){
  86.     message (1, caller, " Can't connect to server: %s ",
  87.          unix_error_string (errno));
  88.     close (my_socket);
  89.     return 0;
  90.     }
  91.     return my_socket;
  92. }
  93.  
  94. /* Extract the hostname and username from the path */
  95. /* path is in the form: [user@]hostname:port/remote-dir, e.g.:
  96.  *
  97.  * ftp://sunsite.unc.edu/pub/linux
  98.  * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
  99.  * ftp://tsx-11.mit.edu:8192/
  100.  * ftp://joe@foo.edu:11321/private
  101.  *
  102.  * If the user is empty, e.g. ftp://@roxanne/private, then your login name
  103.  * is supplied.
  104.  * */
  105.  
  106. char *get_host_and_username (char *path, char **host, char **user, int *port,
  107.                  int default_port, int default_is_anon,
  108.                  char **pass)
  109. {
  110.     struct passwd *passwd_info;
  111.     char *p, *q;
  112.  
  113.     *pass = NULL;
  114.     *port = default_port;
  115.     for (p = path; ((*p != '/') && (*p != ':')) && *p; p++);
  116.  
  117.     q = strchr (path, '@');
  118.     if (q != NULL && q < p) {
  119.         if (q == path)
  120.             *user = NULL;
  121.         else {
  122.             *user = (char *) xmalloc (q  - path + 1, "get_host_and_username");
  123.             strncpy (*user, path, q - path);
  124.             (*user) [q - path] = 0;
  125.         }
  126.         q++;
  127.         *host = (char *) xmalloc (p - q + 1, "get_host_and_username");
  128.         strncpy (*host, q, p - q);
  129.         (*host) [p-q] = 0;
  130.     } else {
  131.         *host = (char *) xmalloc (p - path + 1, "get_host_and_username");
  132.         strncpy (*host, path, p - path);
  133.         (*host) [p-path] = 0;
  134.         *user = NULL;
  135. #ifdef USE_NETRC
  136.         if (use_netrc)
  137.             if (lookup_netrc (*host, user, pass) < 0) {
  138.                 if (*user) { free (*user); *user = NULL; }
  139.                 if (*pass) { free (*pass); *pass = NULL; }
  140.             }
  141. #endif
  142.         if (*user == NULL && default_is_anon)
  143.         *user = strdup ("anonymous");
  144.     }
  145.  
  146.     /* If we got a port spec ... */
  147.     if (*p == ':'){
  148.     
  149.     q = ++p;
  150.  
  151.     for (;*q != '/' && *q; q++)
  152.         ;
  153.     
  154.     if (!*q && q == p)
  155.         return 0;
  156.  
  157.     *port = atoi (p);
  158.     
  159.     if (*port <= 0 || *port >= 65536)
  160.         *port = 21;
  161.     p = q;
  162.     }
  163.  
  164.     if (!*user){
  165.     if ((passwd_info = getpwuid (geteuid ())) == NULL)
  166.         *user = strdup ("anonymous");
  167.     else {
  168.         *user = strdup (passwd_info->pw_name);
  169.     }
  170.     endpwent ();
  171.     }
  172.     if (p && *p)
  173.     return strdup (p);
  174.      else
  175.     return strdup ("/");
  176. }
  177.